﻿using System;
using System.Globalization;
using System.Resources;
using System.ComponentModel;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Threading;

#region [Resources]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.calendar.js", "text/javascript")]
//[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Languages.calendar-en.js", "text/javascript")]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Languages.calendar-tw.js", "text/javascript")]

[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.theme.css", "text/css", PerformSubstitution = true)]     
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Blue.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Blue2.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Brown.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Green.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.System.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Tas.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Win2k1.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Win2k2.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Win2kCold1.theme.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AjaxControlExtenders.DateTimePicker.Skins.Win2kCold2.theme.css", "text/css", PerformSubstitution = true)]

[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.active-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.dark-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.hover-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.menuarrow.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.normal-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.rowhover-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.status-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.title-bg.gif", "image/gif")]
[assembly: System.Web.UI.WebResource("AjaxControlExtenders.DateTimePicker.Skins.Aqua.today-bg.gif", "image/gif")]
#endregion

namespace AjaxControlExtenders
{
    /// <summary>
    /// ExtenderControl1 的摘要描述
    /// </summary>
    [
        TargetControlType(typeof(Control))
    ]        
    public class DateTimePickerExtender : ExtenderControl
    {        
        [DefaultValue("Br")]
        public string Align
        {
            get { return GetPropertyValue<string>("Align", "Br"); }
            set { SetPropertyValue<string>("Align", value); }
        }

        [DefaultValue("%Y-%m-%d")]
        public string Format
        {
            get { return GetPropertyValue<string>("Format", "%Y-%m-%d"); }
            set { SetPropertyValue<string>("Format", value); }
        }
     
        [IDReferenceProperty(typeof(Control))]
        [DefaultValue(null)]
        public string PopupButtonID
        {
            get { return GetPropertyValue<string>("PopupButtonID", null); }
            set { SetPropertyValue<string>("PopupButtonID", value); }
        }

        [DefaultValue(0)]
        public int FirstDayOfWeek
        {
            get { return GetPropertyValue<int>("FirstDayOfWeek", 0); }
            set { SetPropertyValue<int>("FirstDayOfWeek", value); }
        }

        [DefaultValue(false)]
        public bool ShowsTime
        {
            get { return GetPropertyValue<bool>("ShowsTime", false); }
            set { SetPropertyValue<bool>("ShowsTime", value); }
        }

        [DefaultValue(false)]
        public bool ShowsOtherMonths
        {
            get { return GetPropertyValue<bool>("ShowsOtherMonths", false); }
            set { SetPropertyValue<bool>("ShowsOtherMonths", value); }
        }

        [DefaultValue(true)]       
        public bool SingleClick
        {
            get { return GetPropertyValue<bool>("SingleClick", true); }
            set { SetPropertyValue<bool>("SingleClick", value); }
        }

        [DefaultValue("12")]
        public string TimeMode
        {
            get { return GetPropertyValue<string>("TimeMode", "12"); }
            set { SetPropertyValue<string>("TimeMode", value); }
        }

        [DefaultValue(CalendarTheme.Aqua)]
        public CalendarTheme Theme
        {
            get { return GetPropertyValue<CalendarTheme>("Theme", CalendarTheme.Aqua); }
            set { SetPropertyValue<CalendarTheme>("Theme", value); }
        }

        [DefaultValue(true)]
        public bool WeekNumbers
        {
            get { return GetPropertyValue<bool>("WeekNumbers", true); }
            set { SetPropertyValue<bool>("WeekNumbers", value); }
        }

        private void SetPropertyValue<T>(string propertyName, T value)
        {
            ViewState[propertyName] = value;
        }

        protected T GetPropertyValue<T>(string propertyName, T nullValue)
        {
            if (ViewState[propertyName] == null)
            {
                return nullValue;
            }
            return (T)ViewState[propertyName];
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // Register CSS styles
            if (!Page.Items.Contains("DateTimePickerStyles"))
            {                
                Page.Items["DateTimePickerStyles"] = true;
                HtmlLink cssLink = new HtmlLink();
                cssLink.Href = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), StringValueAttribute.GetStringValue(this.Theme));
                cssLink.Attributes.Add("type", "text/css");
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("media", "all");
                this.Page.Header.Controls.Add(cssLink);               
            }                        
        }

        protected override IEnumerable<ScriptDescriptor>
                GetScriptDescriptors(System.Web.UI.Control targetControl)
        {
            if (!targetControl.Visible)
                yield break;
            
            ScriptBehaviorDescriptor descriptor = new ScriptBehaviorDescriptor("AjaxControlExtenders.DateTimePickerBehavior", targetControl.ClientID);

            // Add Properties
            descriptor.AddProperty("align", Align);            
            descriptor.AddProperty("format", Format);
            descriptor.AddProperty("firstDayOfWeek", FirstDayOfWeek);

            if (!string.IsNullOrEmpty(PopupButtonID))
            {
                Control button = this.FindControl(PopupButtonID);
                if (button == null)
                    throw new ArgumentException(string.Format("A control with ID '{0}' could not be found.", PopupButtonID));
                  
                descriptor.AddElementProperty("button", button.ClientID);                
            }
            else
            {
                descriptor.AddProperty("button", null);
            }
            descriptor.AddProperty("showsTime", ShowsTime);
            descriptor.AddProperty("singleClick", SingleClick);
            descriptor.AddProperty("showsOtherMonths", ShowsOtherMonths);            
            descriptor.AddProperty("timeMode", TimeMode);
            descriptor.AddProperty("weekNumbers", WeekNumbers);
            yield return descriptor;
        }

        // 產生指令碼參考
        protected override IEnumerable<ScriptReference>
                GetScriptReferences()
        {            
            yield return new ScriptReference("AjaxControlExtenders.DateTimePicker.calendar.js", this.GetType().Assembly.FullName);
            yield return new ScriptReference("AjaxControlExtenders.DateTimePicker.Languages.calendar-tw.js", this.GetType().Assembly.FullName);            
            yield return new ScriptReference("AjaxControlExtenders.DateTimePicker.DateTimePickerBehavior.js", this.GetType().Assembly.FullName);
        }
        
    }

    public enum CalendarTheme
    {
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Aqua.theme.css")]
        Aqua,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Blue.theme.css")]
        Blue,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Blue2.theme.css")]
        Blue2,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Brown.theme.css")]
        Brown,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Green.theme.css")]
        Green,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.System.theme.css")]
        System,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Tas.theme.css")]
        Tas,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Win2k1.theme.css")]
        Win2k1,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Win2k2.theme.css")]
        Win2k2,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Win2kCold1.theme.css")]
        Win2kCold1,
        [StringValue("AjaxControlExtenders.DateTimePicker.Skins.Win2kCold2.theme.css")]
        Win2kCold2
    }

}